<?php

class Core_Router {

  const CONTROLLER_OFFLINE = 'Offline';
  const CONTROLLER_FRONTEND = 'Frontend';
  const CONTROLLER_BACKEND = 'Backend';
  const CONTROLLER_PANEL = 'Panel';
  const CONTROLLER_SOAPSERVER = 'Soapserver';
  const CONTROLLER_UPLOAD = 'Upload';
  const CONTROLLER_REPORTS = 'Reports';
  const ACTION_DEFAULT = 'index';
  const ACTION_LOGIN = 'login';
  const MODULE_DAMAGES = 'damages';
  const MODULE_EMPEVAL = 'empeval';
  const MODULE_RCP = 'rcp';

  protected $router;
  protected $_config;

  public function __construct() {
    $this->_config = Zend_Registry::get('appConfig');
    $this->router = Zend_Controller_Front::getInstance()->getRouter();
    if (Core_Core::getInstance()->getConfigs()->isOffline()) {
      $this->setOfflineRoutes();
    } else {
      $this->setFrontendRoute();
      $this->setBackendRoute();
      $this->setPanelRoute();
      $this->setSoapserverRoute();
      $this->setDamagesHostRoute();
      $this->setDamagesIpRoute();
      $this->setEmpevalHostRoute();
      $this->setEmpevalBeRoute();
      $this->setEmpevalUploadRoute();
      $this->setEmpevalReportsRoute();
      $this->setRcpHostRoute();
      $this->setRcpBeRoute();
    }
  }

  protected function setOfflineRoutes() {
    $route = new Zend_Controller_Router_Route('*', array(
                'module' => 'default',
                'controller' => self::CONTROLLER_OFFLINE,
                'action' => self::ACTION_DEFAULT
            ));
    $this->router->addRoute('offline', $route);
  }

  private function setFrontendRoute() {
    $route = new Zend_Controller_Router_Route('*', array(
                'controller' => self::CONTROLLER_FRONTEND,
                'action' => self::ACTION_DEFAULT
            ));
    $this->router->addRoute('frontend', $route);
  }

  private function setBackendRoute() {
    $route = new Zend_Controller_Router_Route('/be/:action/*', array(
                'controller' => self::CONTROLLER_BACKEND,
                'action' => self::ACTION_DEFAULT
            ));
    $this->router->addRoute('backend', $route);
  }

  private function setPanelRoute() {
    $route = new Zend_Controller_Router_Route('/panel/:action/*', array(
                'controller' => self::CONTROLLER_PANEL,
                'action' => self::ACTION_LOGIN
            ));
    $this->router->addRoute('register', $route);
  }

  private function setSoapserverRoute() {
    $route = new Zend_Controller_Router_Route('/soap/:action/*', array(
                'controller' => self::CONTROLLER_SOAPSERVER,
                'action' => self::ACTION_LOGIN
            ));
    $this->router->addRoute('soap', $route);
  }

  public function setDamagesHostRoute() {
    $hostRoute = new Zend_Controller_Router_Route_Hostname($this->_config->app->modules->damages->host);
    $route = new Zend_Controller_Router_Route('*', array(
                'controller' => self::CONTROLLER_FRONTEND,
                'action' => self::ACTION_DEFAULT,
                'module' => self::MODULE_DAMAGES
                    )
    );
    $this->router->addRoute('damagesHostRouter', $hostRoute->chain($route));

    $route = new Zend_Controller_Router_Route('/be/:action/*', array(
                'controller' => self::CONTROLLER_BACKEND,
                'action' => self::ACTION_DEFAULT,
                'module' => self::MODULE_DAMAGES
            ));
    $this->router->addRoute('damagesBeHostRouter', $hostRoute->chain($route));
    $route = new Zend_Controller_Router_Route('/upload/:action/*', array(
                'controller' => self::CONTROLLER_UPLOAD,
                'action' => self::ACTION_DEFAULT,
                'module' => self::MODULE_DAMAGES
            ));
    $this->router->addRoute('damagesUploadsHostRouter', $hostRoute->chain($route));
    $route = new Zend_Controller_Router_Route('/bereports/:action/*', array(
                'controller' => self::CONTROLLER_REPORTS,
                'action' => self::ACTION_DEFAULT,
                'module' => self::MODULE_DAMAGES
            ));
    $this->router->addRoute('damagesReportsHostRouter', $hostRoute->chain($route));
  }

  public function setDamagesIpRoute() {
    $hostRoute = new Zend_Controller_Router_Route_Hostname($this->_config->app->modules->damages->ip);
    $route = new Zend_Controller_Router_Route('*', array(
                'controller' => self::CONTROLLER_FRONTEND,
                'action' => self::ACTION_DEFAULT,
                'module' => self::MODULE_DAMAGES
                    )
    );
    $this->router->addRoute('damagesIpRouter', $hostRoute->chain($route));

    $route = new Zend_Controller_Router_Route('/be/:action/*', array(
                'controller' => self::CONTROLLER_BACKEND,
                'action' => self::ACTION_DEFAULT,
                'module' => self::MODULE_DAMAGES
            ));
    $this->router->addRoute('damagesBeIpRouter', $hostRoute->chain($route));
    $route = new Zend_Controller_Router_Route('/upload/:action/*', array(
                'controller' => self::CONTROLLER_UPLOAD,
                'action' => self::ACTION_DEFAULT,
                'module' => self::MODULE_DAMAGES
            ));
    $this->router->addRoute('damagesUploadsIpRouter', $hostRoute->chain($route));
    $route = new Zend_Controller_Router_Route('/bereports/:action/*', array(
                'controller' => self::CONTROLLER_REPORTS,
                'action' => self::ACTION_DEFAULT,
                'module' => self::MODULE_DAMAGES
            ));
    $this->router->addRoute('damagesReportsIpRouter', $hostRoute->chain($route));
  }

  private function setEmpevalHostRoute() {
    $route = new Zend_Controller_Router_Route('/empeval/*', array(
                'controller' => self::CONTROLLER_FRONTEND,
                'action' => self::ACTION_DEFAULT,
                'module' => self::MODULE_EMPEVAL
            ));
    $this->router->addRoute('empeval', $route);
  }

  private function setEmpevalBeRoute() {
    $route = new Zend_Controller_Router_Route('/empeval/be/:action/*', array(
                'controller' => self::CONTROLLER_BACKEND,
                'action' => self::ACTION_DEFAULT,
                'module' => self::MODULE_EMPEVAL
            ));
    $this->router->addRoute('empevalBe', $route);
  }

  private function setEmpevalUploadRoute() {
    $route = new Zend_Controller_Router_Route('/empeval/file/:action/*', array(
                'controller' => self::CONTROLLER_UPLOAD,
                'action' => self::ACTION_DEFAULT,
                'module' => self::MODULE_EMPEVAL
            ));
    $this->router->addRoute('empevalUpload', $route);
  }

  private function setEmpevalReportsRoute() {
    $route = new Zend_Controller_Router_Route('/empeval/report/:action/*', array(
                'controller' => self::CONTROLLER_REPORTS,
                'action' => self::ACTION_DEFAULT,
                'module' => self::MODULE_EMPEVAL
            ));
    $this->router->addRoute('empevalReport', $route);
  }

  private function setRcpHostRoute() {
    $route = new Zend_Controller_Router_Route('/rcp/*', array(
                'controller' => self::CONTROLLER_FRONTEND,
                'action' => self::ACTION_DEFAULT,
                'module' => self::MODULE_RCP
            ));
    $this->router->addRoute('rcp', $route);
  }

  private function setRcpBeRoute() {
    $route = new Zend_Controller_Router_Route('/rcp/be/:action/*', array(
                'controller' => self::CONTROLLER_BACKEND,
                'action' => self::ACTION_DEFAULT,
                'module' => self::MODULE_RCP
            ));
    $this->router->addRoute('rcpBe', $route);
  }

}

?>